package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.*;
import com.tanhua.model.vo.SoulQuestionnaireVo;
import com.tanhua.model.vo.SoulQuestionsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class QuestionsApiImpl implements QuestionsApi{


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public SoulQuestionnaireVo testSoul(Long userId) {
        // 1.查询soul_recommer的表判断是否存在记录 存在记录推荐等级大于原来的 不存在 推荐最低的等级
        Criteria criteria = Criteria.where("userId").is(userId.toString());
        SoulRecommend one = mongoTemplate.findOne(Query.query(criteria), SoulRecommend.class);

        SoulQuestionnaireVo soulQuestionnaireVo = new SoulQuestionnaireVo();
        List<SoulQuestionsVo> list = new ArrayList<>();
        if(one==null){
            Criteria criteria1 = Criteria.where("options").is(1);
            List<Questions> questions = mongoTemplate.find(Query.query(criteria1), Questions.class);
            for (Questions question : questions) {
                SoulQuestionsVo<Option> questionsVo = new SoulQuestionsVo<>();
                questionsVo.setQuestion(question.getQuestion());
                Criteria questionId = Criteria.where("questionId").is(question.getQId());
                List<Option> options = mongoTemplate.find(Query.query(questionId), Option.class);
                questionsVo.setOptions(options);
                list.add(questionsVo);
            }
            Criteria near = Criteria.where("level").is("1");
            Questionnaire questionnaire = mongoTemplate.findOne(Query.query(near), Questionnaire.class);

            soulQuestionnaireVo.setQuestions(questions);
            soulQuestionnaireVo.setCover(questionnaire.getCover());
            soulQuestionnaireVo.setId(questionnaire.getLevel().toString());
            soulQuestionnaireVo.setName(questionnaire.getName());
            soulQuestionnaireVo.setLevel(questionnaire.getLevel());
            soulQuestionnaireVo.setStar(Integer.valueOf(questionnaire.getStar()));
            soulQuestionnaireVo.setIsLock(0);
            return soulQuestionnaireVo;
        }
        String level = one.getLevel();
        Criteria criteria1 = Criteria.where("level").is(level);
        Questionnaire questions = mongoTemplate.findOne(Query.query(criteria1), Questionnaire.class);
        Criteria c1 = Criteria.where("options").is(questions.getLevel());
        List<Questions> questions1 = mongoTemplate.find(Query.query(c1), Questions.class);

        /*for (Questions question : questions) {
            SoulQuestionsVo<Option> questionsVo = new SoulQuestionsVo<>();
            questionsVo.setQuestion(question.getQuestion());
            Criteria questionId = Criteria.where("questionId").is(question.getId());
            List<Option> options = mongoTemplate.find(Query.query(questionId), Option.class);
            questionsVo.setOptions(options);
        }*/

        return null;
    }
}
